package com.waterworld.watch.mine.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.waterworld.watch.R;
import com.waterworld.watch.common.activity.BaseActivity;
import com.waterworld.watch.common.bean.IntimateSetBean;
import com.waterworld.watch.common.bean.UserInfoBean;
import com.waterworld.watch.common.net.BaseObserverListener;
import com.waterworld.watch.common.util.ScreenAdapterUtil;
import com.waterworld.watch.common.util.ToastUtils;
import com.waterworld.watch.home.activity.HomePagerActivity;
import com.waterworld.watch.mine.interfaces.IMineManager;
import com.waterworld.watch.mine.manager.MineManager;

import butterknife.BindView;
import butterknife.ButterKnife;

public class IntimateActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.header_parent)
    LinearLayout header_parent;

    @BindView(R.id.header_back)
    ImageView header_back;

    @BindView(R.id.header_title)
    TextView header_title;

    @BindView(R.id.cb_shield_strange_phone)
    CheckBox cb_shield_strange_phone;

    @BindView(R.id.cb_reserve_electricity)
    CheckBox cb_reserve_electricity;

    @BindView(R.id.cb_power_saving_mode)
    CheckBox cb_power_saving_mode;

    @BindView(R.id.cb_call_location)
    CheckBox cb_call_location;

    @BindView(R.id.cb_auto_answer)
    CheckBox cb_auto_answer;

    @BindView(R.id.cb_ban_shutDown)
    CheckBox cb_ban_shutDown;

    @BindView(R.id.cb_vol_call)
    CheckBox cb_vol_call;

    private IMineManager iMineManager = MineManager.getInstance();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intimate);
        ButterKnife.bind(this);
        initView();
    }


    private void initView() {
        setViewSize(header_parent, ViewGroup.LayoutParams.MATCH_PARENT, ScreenAdapterUtil.getHeightPx(this) / 10);
        header_title.setText(getString(R.string.intimate_set));
        header_back.setVisibility(View.VISIBLE);
        header_title.setVisibility(View.VISIBLE);

        cb_shield_strange_phone.setChecked(IntimateSetBean.getInstance().isShieldStrangeCall());
        cb_reserve_electricity.setChecked(IntimateSetBean.getInstance().isReservePower());
        cb_power_saving_mode.setChecked(IntimateSetBean.getInstance().isPowerSaving());
        cb_call_location.setChecked(IntimateSetBean.getInstance().isCallPosition());
        cb_auto_answer.setChecked(IntimateSetBean.getInstance().isAutoAnswer());
        cb_ban_shutDown.setChecked(IntimateSetBean.getInstance().isNoShutdown());
        cb_vol_call.setChecked(IntimateSetBean.getInstance().isEnableVolte());

        header_back.setOnClickListener(this);

        if (UserInfoBean.getInstance().getWatchUser().getUserRole() == 0) {
            cb_shield_strange_phone.setClickable(true);
            cb_reserve_electricity.setClickable(true);
            cb_power_saving_mode.setClickable(true);
            cb_call_location.setClickable(true);
            cb_auto_answer.setClickable(true);
            cb_ban_shutDown.setClickable(true);
            cb_vol_call.setClickable(true);

            cb_shield_strange_phone.setOnClickListener(this);
            cb_reserve_electricity.setOnClickListener(this);
            cb_power_saving_mode.setOnClickListener(this);
            cb_call_location.setOnClickListener(this);
            cb_auto_answer.setOnClickListener(this);
            cb_ban_shutDown.setOnClickListener(this);
            cb_vol_call.setOnClickListener(this);
        }

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.header_back:
                finish();
                break;
            case R.id.cb_shield_strange_phone:
                IntimateSetBean.getInstance().setShieldStrangeCall(cb_shield_strange_phone.isChecked());
                updateIntimateSet(null,null,null,null,cb_shield_strange_phone.isChecked(),null,null,null,null,null,null);
                break;
            case R.id.cb_reserve_electricity:
                IntimateSetBean.getInstance().setReservePower(cb_reserve_electricity.isChecked());
                updateIntimateSet(cb_reserve_electricity.isChecked(),null,null,null,null,null,null,null,null,null,null);
                break;
            case R.id.cb_power_saving_mode:
                IntimateSetBean.getInstance().setPowerSaving(cb_power_saving_mode.isChecked());
                updateIntimateSet(null,cb_power_saving_mode.isChecked(),null,null,null,null,null,null,null,null,null);
                break;
            case R.id.cb_call_location:
                IntimateSetBean.getInstance().setCallPosition(cb_call_location.isChecked());
                updateIntimateSet(null,null,cb_call_location.isChecked(),null,null,null,null,null,null,null,null);
                break;
            case R.id.cb_auto_answer:
                IntimateSetBean.getInstance().setAutoAnswer(cb_auto_answer.isChecked());
                updateIntimateSet(null,null,null,cb_auto_answer.isChecked(),null,null,null,null,null,null,null);
                break;
            case R.id.cb_ban_shutDown:
                IntimateSetBean.getInstance().setNoShutdown(cb_ban_shutDown.isChecked());
                updateIntimateSet(null,null,null,null,null,null,null,cb_ban_shutDown.isChecked(),null,null,null);
                break;
            case R.id.cb_vol_call:
                IntimateSetBean.getInstance().setEnableVolte(cb_vol_call.isChecked());
                updateIntimateSet(null,null,null,null,null,null,null,null,cb_vol_call.isChecked(),null,null);
                break;
            default:
                break;

        }
    }

    private void updateIntimateSet(Boolean isReservePower, Boolean isPowerSaving, Boolean isCallPosition, Boolean isAutoAnswer, Boolean isShieldStrangeCall,
                    Boolean isAutoBoot, Boolean isAutoShutdown, Boolean isNoShutdown, Boolean isEnableVolte, String bootTime,
                    String shutdownTime){
        if (iMineManager != null) {
            iMineManager.updateIntimateSet(isReservePower, isPowerSaving, isCallPosition, isAutoAnswer, isShieldStrangeCall, isAutoBoot, isAutoShutdown,
                    isNoShutdown, isEnableVolte, bootTime, shutdownTime, new BaseObserverListener() {
                        @Override
                        public void onCompleted() {
                        }

                        @Override
                        public void onError(Throwable e) {
                            ToastUtils.showShort(IntimateActivity.this, getString(R.string.data_error));
                            Log.e("nh",e.toString());
                        }

                        @Override
                        public void onNext(Object o) {

                        }
                    });
        }
    }
}
